﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class CustomAdminPageDal : Base
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            ControlPath,
            Notes,
            Sequence,
            Section
        }

        protected override void ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<string>(Fields.ControlPath, reader, table);
            GetReaderValue<string>(Fields.Notes, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
            GetReaderValue<byte>(Fields.Section, reader, table);
        }

        public Hashtable[] GetAllBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int siteId, string name, string controlPath, string notes, ref int sequence, byte section, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "ControlPath", controlPath, System.Data.SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "Notes", notes, System.Data.SqlDbType.VarChar, 2000);
            AddInputParameter(cmd, "Sequence", sequence, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "Section", section, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, System.Data.SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "SequenceOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", System.Data.SqlDbType.Int);
            base.ExecuteCommand(cmd);
            id = GetOutputValue<int>(cmd, "IdOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            concurencyId = GetOutputValue<int>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, System.Data.SqlDbType.Timestamp);
            base.ExecuteCommand(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByNameAndSiteId(string name, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetByNameAndSiteId");
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }
    }
}
